package fu.k4c.vTeam.Main;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import fu.k4c.vTeam.R;
import fu.k4c.vTeam.Main.ViewPlaceActivity.loadMoreList;
import fu.k4c.vTeam.Main.ViewPlaceActivity.onLoadScrollListener;
import fu.k4c.vTeam.Manage.CreateEventActivity;
import fu.k4c.vTeam.Object.EventDetails;
import fu.k4c.vTeam.Object.ItemEvent;
import fu.k4c.vTeam.Object.ItemViewPlace;
import fu.k4c.vTeam.ViewDetails.EventTabLayoutActivity;

public class ViewEventActivity extends BaseActivity {
	String eventStatus = "";
	String eventPlace = "";
	GridView gv_event;
	GridView gv_event2;
	EditText et_search;
	List<ItemEvent> yourClassList;
	Spinner Cityspinner;
	private DisplayImageOptions options;
	int isAction = 1;
	int isCity = 1;
	Spinner StatusSpinner;
	String email = "";
	String Status;
	int page = 1;
	int nextPage=1;
	String typeevent="";
	String cID="";
	boolean loading = false;	
	int textlength = 0;
	ProgressDialog proDialog;
	static final private int CHOOSE_ADD = 1;
	int CURRENT_TAG = 0;
	static final int TAG_GUEST = 0;
	static final int TAG_USER = 1;
	ListEvent adapter;
	String cities[] = { "Tất cả", "Hà Nội", "TP Hồ Chí Minh", "Khác" };
	String status[] = { "Đang diễn ra", "Sắp diễn ra", "Đã diễn ra" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_event_activity);
		gv_event = (GridView) findViewById(R.id.gv_event);
		gv_event.setOnScrollListener(new onLoadScrollListener());
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.stub_image)
				.showImageForEmptyUri(R.drawable.image_for_empty_url)
				.cacheInMemory().cacheOnDisc()
				.displayer(new RoundedBitmapDisplayer(20)).build();
		proDialog = ProgressDialog.show(ViewEventActivity.this, "",
				"Đang tải dữ liệu...");
		new Thread() {
			public void run() {

				try {
					sleep(1000);

				} catch (Exception e) {
					System.out.print(e.toString());
				}
				// dismiss the progressdialog
				proDialog.dismiss();
			}
		}.start();
		Button btn_back = (Button) findViewById(R.id.btn_view_event_back);
		btn_back.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		Bundle data = getIntent().getExtras();
		if (data != null) {
			email = data.getString("email");
			Status = data.getString("status");
			if (data.getString("email").equals("")) {
				CURRENT_TAG = TAG_GUEST;
			} else {
				CURRENT_TAG = TAG_USER;
			}
		}
		Button btn_create = (Button) findViewById(R.id.btn_event_create_events);
		btn_create.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (CURRENT_TAG == TAG_GUEST) {
					Toast.makeText(
							getApplicationContext(),
							"Bạn phải đăng nhập để có thể sử dụng chức năng này",
							Toast.LENGTH_LONG).show();
				} else {
					Intent i = new Intent(ViewEventActivity.this,
							CreateEventActivity.class);
					i.putExtra("email", email);
					startActivityForResult(i, CHOOSE_ADD);
				}
			}
		});

		eventStatus = "3";
		eventPlace = "4";
		getdata(eventStatus, eventPlace);
		// getdata(eventStatus,eventPlace);
		Cityspinner = (Spinner) findViewById(R.id.sp_event_place);
		ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item, cities);
		spinnerArrayAdapter1
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		Cityspinner.setAdapter(spinnerArrayAdapter1);
		Cityspinner
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> parent,
							View view, int pos, long id) {
						// Toast.makeText(getApplicationContext(), pos + "",
						// Toast.LENGTH_LONG).show();
						if (isCity > 1) {
							proDialog = ProgressDialog.show(
									ViewEventActivity.this, "",
									"Đang tải dữ liệu...");
							new Thread() {
								public void run() {

									try {
										sleep(1000);

									} catch (Exception e) {

									}
									proDialog.dismiss();
								}
							}.start();
							page = 1;
							nextPage = 1;
							Object item = parent.getItemAtPosition(pos);
							String Place = item.toString();
							if (Place.equals("Hà Nội")) {
								eventPlace = "1";
								getdata(eventStatus, eventPlace);
							} else if (Place.equals("TP Hồ Chí Minh")) {
								eventPlace = "2";
								getdata(eventStatus, eventPlace);
							} else if (Place.equals("Khác")) {
								eventPlace = "3";
								getdata(eventStatus, eventPlace);
							} else {
								eventPlace = "4";
								getdata(eventStatus, eventPlace);
							}

						}
						isCity++;

					}

					public void onNothingSelected(AdapterView<?> parent) {
					}
				});
		StatusSpinner = (Spinner) findViewById(R.id.sp_event_status);
		ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(
				this, android.R.layout.simple_spinner_item, status);
		spinnerArrayAdapter2
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		StatusSpinner.setAdapter(spinnerArrayAdapter2);
		StatusSpinner
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> parent,
							View view, int pos, long id) {
						// Toast.makeText(getApplicationContext(), pos + "",
						// Toast.LENGTH_LONG).show();
						if (isAction > 1) {
							proDialog = ProgressDialog.show(
									ViewEventActivity.this, "",
									"Đang tải dữ liệu...");
							new Thread() {
								public void run() {

									try {
										sleep(1000);

									} catch (Exception e) {
										System.out.print(e.toString());
									}
									// dismiss the progressdialog
									proDialog.dismiss();
								}
							}.start();
							page = 1;
							nextPage = 1;
							Object item = parent.getItemAtPosition(pos);
							String status = item.toString();
							if (status.equals("Đã diễn ra")) {
								eventStatus = "1";
								getdata(eventStatus, eventPlace);
							} else if (status.equals("Đang diễn ra")) {
								eventStatus = "3";
								getdata(eventStatus, eventPlace);

							} else {
								eventStatus = "2";
								getdata(eventStatus, eventPlace);
							}
						}
						isAction++;

					}

					public void onNothingSelected(AdapterView<?> parent) {
					}
				});
		et_search = (EditText) findViewById(R.id.et_view_event_search);
		et_search.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence cs, int arg1, int arg2,
					int arg3) {
				// When user changed the Text
				textlength = et_search.getText().length();
				final List<ItemEvent> listSearch = new ArrayList<ItemEvent>();
				;
				for (int i = 0; i < yourClassList.size(); i++) {
					if (yourClassList.get(i).getName().toLowerCase()
							.indexOf(et_search.getText().toString()) >= 0
							|| yourClassList.get(i).getAddress().toLowerCase()
									.indexOf(et_search.getText().toString()) >= 0) {
						listSearch.add(yourClassList.get(i));
					}
				}
				ListEvent adapter = new ListEvent(ViewEventActivity.this,
						listSearch);
				gv_event.setAdapter(adapter);
				gv_event.setOnItemClickListener(new OnItemClickListener() {

					public void onItemClick(AdapterView<?> arg0, View v,
							int position, long id) {
						// TODO Auto-generated method stub
						proDialog = ProgressDialog.show(ViewEventActivity.this,
								"", "Đang tải dữ liệu...");
						new Thread() {
							public void run() {

								try {
									sleep(2000);

								} catch (Exception e) {
									System.out.print(e.toString());
								}
								// dismiss the progressdialog
								proDialog.dismiss();
							}
						}.start();
						SchemeRegistry registry = new SchemeRegistry();
						registry.register(new Scheme("http", PlainSocketFactory
								.getSocketFactory(), 80));
						HttpParams httpParams = new BasicHttpParams();
						ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(
								httpParams, registry);
						HttpClient client = new DefaultHttpClient(connMgr,
								httpParams);
						HttpPost post = new HttpPost(
								getString(R.string.connectionString)
										+ "vLocationService/getdetailevent");
						post.setHeader("Accept", "application/json");
						post.setHeader("Content-type", "application/json");
						HashMap<String, Object> postParameters = new HashMap<String, Object>();
						postParameters.put("IDofItemRevision",
								listSearch.get(position).getID());

						if (CURRENT_TAG == TAG_USER) {
							postParameters.put("username", email);
						} else {
							postParameters.put("username", "Guest");
						}
						Gson gson = new Gson();
						try {
							String jsonParameters = gson.toJson(postParameters);
							post.setEntity(new StringEntity(jsonParameters));
							HttpResponse response = client.execute(post);
							HttpEntity entity = response.getEntity();
							String jsonresult = EntityUtils.toString(entity);
							EventDetails mDetails = gson.fromJson(jsonresult,
									EventDetails.class);
							Intent i = new Intent(ViewEventActivity.this,
									EventTabLayoutActivity.class);
							i.putExtra("address", ""
									+ listSearch.get(position).getAddress());
							i.putExtra("placename", mDetails.getReName());
							i.putExtra("placetel", mDetails.getRePhone());
							i.putExtra("likenumber", mDetails.getReslVote());
							i.putExtra("visitnumber", mDetails.getReslVisit());
							i.putExtra("createby", mDetails.getReCreatBy());
							i.putExtra("des", mDetails.getReDescription());
							i.putExtra("lat", mDetails.getRelat());
							i.putExtra("long", mDetails.getRelong());
							i.putExtra("visit", mDetails.isReDaDen());
							i.putExtra("like", mDetails.isReLike());
							i.putExtra("begin", mDetails.getStartDate());
							i.putExtra("end", mDetails.getEndDate());
							i.putExtra("IDofItemRevision",
									listSearch.get(position).getID());
							i.putExtra("views",
									"" + mDetails.getSolgnguoiview());
							i.putExtra("avatar", "" + mDetails.getImageUrl());
							i.putExtra("createbyemail",
									"" + mDetails.getEmail());
							if (CURRENT_TAG == TAG_USER) {
								i.putExtra("username", email);
							} else {
								i.putExtra("username", "Guest");
							}
							startActivity(i);
						} catch (Exception e) {
							// TODO: handle exception
							Toast.makeText(getApplicationContext(),
									e.toString(), Toast.LENGTH_LONG).show();
						}
					}
				});
			}

			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK && requestCode == CHOOSE_ADD) {
			proDialog = ProgressDialog.show(ViewEventActivity.this, "",
					"Đang tải dữ liệu...");
			new Thread() {
				public void run() {

					try {
						sleep(1000);

					} catch (Exception e) {
						System.out.print(e.toString());
					}
					// dismiss the progressdialog
					proDialog.dismiss();
				}
			}.start();

			getdata(eventStatus, eventPlace);

		}
		if (requestCode == RESULT_CANCELED) {

		}
	}

	private void getdata(String TypeOfEvent, String CityID) {
		// TODO Auto-generated method stub
		try {
			cID = CityID;
			typeevent = TypeOfEvent;
			SchemeRegistry registry = new SchemeRegistry();
			registry.register(new Scheme("http", PlainSocketFactory
					.getSocketFactory(), 80));
			HttpParams httpParams = new BasicHttpParams();
			ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(
					httpParams, registry);
			HttpClient client = new DefaultHttpClient(connMgr, httpParams);
			HttpPost post = new HttpPost(getString(R.string.connectionString)
					+ "vLocationService/geteventbytime");
			post.setHeader("Accept", "application/json");
			post.setHeader("Content-type", "application/json");
			HashMap<String, Object> postParameters = new HashMap<String, Object>();
			postParameters.put("TypeOfEvent", TypeOfEvent);
			postParameters.put("CityID", CityID);
			postParameters.put("Page", ""+page);
			Gson gson = new Gson();
			String jsonParameters = gson.toJson(postParameters);
			post.setEntity(new StringEntity(jsonParameters));
			HttpResponse response = client.execute(post);
			HttpEntity entity = response.getEntity();
			String jsonresult = EntityUtils.toString(entity);
			try {
				Type listType = new TypeToken<ArrayList<ItemEvent>>() {
				}.getType();
				yourClassList = new Gson().fromJson(jsonresult, listType);
				try {
					nextPage = Integer.parseInt(yourClassList.get(0).getPage());
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				if (yourClassList.size() == 0) {
					Toast.makeText(getApplicationContext(),
							"Không có kết quả nào", Toast.LENGTH_LONG).show();
				}
				adapter = new ListEvent(this, yourClassList);
				gv_event.setAdapter(adapter);
				gv_event.setOnItemClickListener(new OnItemClickListener() {

					public void onItemClick(AdapterView<?> arg0, View v,
							int position, long id) {
						// TODO Auto-generated method stub
						proDialog = ProgressDialog.show(ViewEventActivity.this,
								"", "Đang tải dữ liệu...");
						new Thread() {
							public void run() {

								try {
									sleep(2000);

								} catch (Exception e) {
									System.out.print(e.toString());
								}
								proDialog.dismiss();
							}
						}.start();
						SchemeRegistry registry = new SchemeRegistry();
						registry.register(new Scheme("http", PlainSocketFactory
								.getSocketFactory(), 80));
						HttpParams httpParams = new BasicHttpParams();
						ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(
								httpParams, registry);
						HttpClient client = new DefaultHttpClient(connMgr,
								httpParams);
						HttpPost post = new HttpPost(
								getString(R.string.connectionString)
										+ "vLocationService/getdetailevent");
						post.setHeader("Accept", "application/json");
						post.setHeader("Content-type", "application/json");
						HashMap<String, Object> postParameters = new HashMap<String, Object>();
						postParameters.put("IDofItemRevision", yourClassList
								.get(position).getID());
						postParameters.put("username", email);
						Gson gson = new Gson();
						try {
							String jsonParameters = gson.toJson(postParameters);
							post.setEntity(new StringEntity(jsonParameters));
							HttpResponse response = client.execute(post);
							HttpEntity entity = response.getEntity();
							String jsonresult = EntityUtils.toString(entity);
							EventDetails mDetails = gson.fromJson(jsonresult,
									EventDetails.class);
							Intent i = new Intent(ViewEventActivity.this,
									EventTabLayoutActivity.class);
							System.out.println(mDetails.isReDaDen());
							i.putExtra("address",
									""
											+ yourClassList.get(position)
													.getAddress());
							i.putExtra("placename", mDetails.getReName());
							i.putExtra("placetel", mDetails.getRePhone());
							i.putExtra("likenumber", mDetails.getReslVote());
							i.putExtra("visitnumber", mDetails.getReslVisit());
							i.putExtra("createby", mDetails.getReCreatBy());
							i.putExtra("des", mDetails.getReDescription());
							i.putExtra("lat", mDetails.getRelat());
							i.putExtra("long", mDetails.getRelong());
							i.putExtra("visit", mDetails.isReDaDen());
							i.putExtra("like", mDetails.isReLike());
							i.putExtra("begin", mDetails.getStartDate());
							i.putExtra("end", mDetails.getEndDate());
							i.putExtra("IDofItemRevision",
									yourClassList.get(position).getID());
							i.putExtra("views",
									"" + mDetails.getSolgnguoiview());
							i.putExtra("avatar", "" + mDetails.getImageUrl());
							i.putExtra("createbyemail",
									"" + mDetails.getEmail());
							if (CURRENT_TAG == TAG_USER) {
								i.putExtra("username", email);
							} else {
								i.putExtra("username", "Guest");
							}
							startActivity(i);
						} catch (Exception e) {
							// TODO: handle exception
							Toast.makeText(getApplicationContext(),
									e.toString(), Toast.LENGTH_LONG).show();
						}
					}
				});

			} catch (Exception e) {
				Toast.makeText(getApplicationContext(),
						"Loi : " + e.toString(), Toast.LENGTH_LONG).show();
				// TODO: handle exception
			}
		} catch (Exception e) {
			// TODO: handle exception
			Toast.makeText(getApplicationContext(),
					"Bạn vui lòng kiểm tra lại kết nối internet! ",
					Toast.LENGTH_LONG).show();
		}
	}
	public class onLoadScrollListener implements OnScrollListener {
		int lastinScreen;
		int currentScrollState;
		public onLoadScrollListener() {
	     }
	     public onLoadScrollListener(int lastinScreen) {
	         this.lastinScreen = lastinScreen;
	     }
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			// TODO Auto-generated method stub
			lastinScreen = firstVisibleItem + visibleItemCount;
			Log.i("scroll", "lastinScreen  :"+ lastinScreen);
			Log.d("scroll", "totalItemCount  :"+ totalItemCount);
			if (!loading && (lastinScreen==totalItemCount) && nextPage >=2 && totalItemCount>0){
				page = nextPage;
				Log.d("scroll", "page  :"+ page);
				new loadMoreList().execute();
			}
		}

		public void onScrollStateChanged(AbsListView view, int scrollState) {
			// TODO Auto-generated method stub
			this.currentScrollState = scrollState;
		}
		
	}
	public class loadMoreList extends AsyncTask<Void, Void, Void>{
		String e="";
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			loading = true;
			Log.i("onPre", "da chay vao onPre");
			proDialog = new ProgressDialog(ViewEventActivity.this);
			proDialog.setMessage("Đang kết nối đến máy chủ...");
			proDialog.setCancelable(false);
			proDialog.show();
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
					try {
						SchemeRegistry registry = new SchemeRegistry();
						registry.register(new Scheme("http", PlainSocketFactory
								.getSocketFactory(), 80));
						HttpParams httpParams = new BasicHttpParams();
						ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(
								httpParams, registry);
						HttpClient client = new DefaultHttpClient(connMgr, httpParams);
						HttpPost post = new HttpPost(getString(R.string.connectionString)
								+ "vLocationService/geteventbytime");
						post.setHeader("Accept", "application/json");
						post.setHeader("Content-type", "application/json");
						HashMap<String, Object> postParameters = new HashMap<String, Object>();
						postParameters.put("TypeOfEvent", typeevent);
						postParameters.put("CityID", cID);
						postParameters.put("Page", ""+page);
						Gson gson = new Gson();
						String jsonParameters = gson.toJson(postParameters);
						post.setEntity(new StringEntity(jsonParameters));
						HttpResponse response = client.execute(post);
						HttpEntity entity = response.getEntity();
						String jsonresult = EntityUtils.toString(entity);
						Type listType = new TypeToken<ArrayList<ItemEvent>>() {
						}.getType();
						List<ItemEvent> yourClassList2 = new Gson().fromJson(jsonresult, listType);
						nextPage = Integer.parseInt(yourClassList2.get(0).getPage());
						Log.i("nextPage2", ""+nextPage);
						for(int i = 0; i<=yourClassList2.size();i++)
						{
							yourClassList.add(yourClassList2.get(i));
						}
					} catch (Exception e) {
						// TODO: handle exception
//						this.e=e.toString(); 
					}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			proDialog.dismiss();
			adapter.notifyDataSetChanged();
			loading = false;
//			Toast.makeText(getApplicationContext(), e, Toast.LENGTH_LONG).show();
			super.onPostExecute(result);
		}
		
	}
	public class ListEvent extends BaseAdapter {
		Activity listEvent;
		List<ItemEvent> data;
		LayoutInflater inflater = null;

		private class ViewHolder {
			public TextView tv_name;
			public TextView tv_phone;
			public TextView tv_address;
			public TextView tv_view;
			public ImageView img_avatar;
			public TextView tv_start;
			public TextView tv_end;
		}

		public ListEvent(Activity mlistEvent, List<ItemEvent> mdata) {
			listEvent = mlistEvent;
			data = mdata;
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return data.get(arg0);
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			View view = convertView;
			final ViewHolder holder;
			if (convertView == null) {
				view = getLayoutInflater().inflate(R.layout.row_event, null);
				holder = new ViewHolder();

				holder.tv_name = (TextView) view
						.findViewById(R.id.tv_row_event_name);
				holder.tv_phone = (TextView) view
						.findViewById(R.id.tv_row_event_phone);
				holder.tv_address = (TextView) view
						.findViewById(R.id.tv_row_event_address);
				holder.tv_start = (TextView) view
						.findViewById(R.id.tv_row_event_start);
				holder.tv_end = (TextView) view
						.findViewById(R.id.tv_row_event_end);
				holder.tv_view = (TextView) view
						.findViewById(R.id.tv_row_event_views);
				holder.img_avatar = (ImageView) view
						.findViewById(R.id.imv_row_event);
				view.setTag(holder);
			} else
				holder = (ViewHolder) view.getTag();

			holder.tv_name.setText(data.get(position).getName());
			if (data.get(position).getPhone() != null) {
				holder.tv_phone.setText("Số điện thoại : "
						+ data.get(position).getPhone());
			} else {
				holder.tv_phone.setText("Số điện thoại : N\\A");
			}
			holder.tv_address.setText("Địa chỉ : "
					+ data.get(position).getAddress());
			holder.tv_start.setText("Bắt đầu : "
					+ data.get(position).getStartDate());
			holder.tv_end.setText("Kết thúc : "
					+ data.get(position).getEndDate());
			String imageurl;
			if (data.get(position).getImageUrl() != null) {
				if (data.get(position).getImageUrl().toLowerCase()
						.indexOf("http://") >= 0) {
					imageurl = data.get(position).getImageUrl();

				} else {
					imageurl = getString(R.string.connectionString)
							+ data.get(position).getImageUrl();
				}
			} else {
				imageurl = data.get(position).getImageUrl();
			}
			imageLoader.init(ImageLoaderConfiguration
					.createDefault(getBaseContext()));
			imageLoader.displayImage(imageurl, holder.img_avatar, options,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingComplete(Bitmap loadedImage) {
							Animation anim = AnimationUtils.loadAnimation(
									ViewEventActivity.this, R.anim.fade_in);
							holder.img_avatar.setAnimation(anim);
							anim.start();
						}
					});
			if (data.get(position).getViews() != null) {
				holder.tv_view.setText("Lượt xem : "
						+ data.get(position).getViews());
			} else {
				holder.tv_view.setText("Lượt xem : 0");
			}
			return view;
		}

	}
}
